package com.tuars.instalacionesalberti.connection.bd;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JOptionPane;

public class ConnectionBaseDades {
	
	private String incorrectConection ="Error al establecer la conexion con la base de datos";
	private String titleIncorrectCon = "Error de conexion";
	
	private String errorDriver = "Error al iontentar conectar con el driver MySQL";
	private String titleDriver = "Error Driver";
	
	/**
	 * 
	 * @return conexi�n de la base de datos
	 */
	public Connection Conexion(){
		Connection con = null;
		
		try{
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ALMACEN_ELECTRICA_ALBERTI","root","78217369-E");
		}catch(SQLException e){
			JOptionPane.showMessageDialog(null, incorrectConection, titleIncorrectCon, JOptionPane.ERROR_MESSAGE);
		}catch(ClassNotFoundException e){
			JOptionPane.showMessageDialog(null, errorDriver, titleDriver, JOptionPane.ERROR_MESSAGE);
		}
		
		return con;
		
	}
	
	public int numeroDeFilas(String sql){
		
		Statement st = null;
		ResultSet rs = null;
		int numOfRows = 0;
		try{
			st = Conexion().createStatement();
			rs = st.executeQuery(sql);
			if (rs.last()){
				numOfRows = rs.getRow();
				} else {
				numOfRows = 0;
				}
				rs.first();
				
				rs.close();
				st.close();
				Conexion().close();
		}catch (SQLException e){
			e.printStackTrace();
		}
		return numOfRows;
	}

}
